/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "TypeMeta.h"

#include "MetaArray.h"

#include "../common/BigEndianUnsignedByte.h"
#include "../common/BigEndianUnsignedShort.h"

type_name meta_array_name = { "meta.array" };

int meta_array_bind( type_element * element, type_definition * definition )
{
	meta_array * ma = (meta_array *) element;
	int rc;
	
	rc = type_object_bind( &element->object, definition->library, &meta_array_name );
	if ( rc != TYPE_OK ) return rc;

	rc = type_element_bind( element, definition );
	if ( rc != TYPE_OK ) return rc;
	
	rc = ma->size->element.bind( &(ma->size->element), definition );
	if ( rc != TYPE_OK ) return rc;
	
	rc = ma->type->element.bind( &(ma->type->element), definition );
	if ( rc != TYPE_OK ) return rc;
	
	return TYPE_OK;
}

void meta_array_init( meta_array * ma, meta_expression * size, meta_expression * type )
{
	type_element_init( &(ma->expression.element), &meta_array_bind );
	meta_expression_init( &(ma->expression), meta_array_do_read, meta_array_do_write );

	ma->size = size;
	ma->type = type;
}

void * meta_array_construct( type_list * list, type_map * map, void * data )
{
	type_item * item = type_list_head( list );
	meta_expression * size = (meta_expression *) type_item_get_ptr( item );	
	meta_expression * type;
	meta_array * ma;

	item = type_item_next( item );
	type = (meta_expression *) type_item_get_ptr( item );
	
	ma = (meta_array*) type_malloc( sizeof( meta_array ) );
	meta_array_init( ma, size, type );
	return ma;
}

int meta_array_read( type_input_stream * in, type_element * element, void * data )
{
	TRACE( "meta_array_read" );
	return type_reader_auto( in, element, meta_array_construct, 0 );
}

int meta_array_write( type_output_stream * out, void * o, type_element * element, void * data )
{
	meta_array * ma = (meta_array *) o;

	TRACE( "meta_array_write" );
	
	type_output_write_named_object( out, &meta_expression_name, ma->size );
	type_output_write_named_object( out, &meta_expression_name, ma->type );
	return TYPE_OK;	
}

int meta_array_do_read( type_input_stream * in, type_element * element, void * data )
{
	meta_array * ma = (meta_array *) element;
	type_stack * stack = type_input_stack( in );
	type_list * list = type_stack_get_list( &in->stack );
	uint32_t x;
	int rc;
	uint32_t size;
	type_element * size_type;
	type_name * element_name;

	TRACE( "meta_array_do_read" );
		
	type_list_init( list );
	
	type_stack_push( stack );
	rc = ma->size->doRead( in, (type_element *) ma->size, NULL );
	if ( rc != TYPE_OK ) return rc;
	
	
	size_type = type_stack_type( stack );
	element_name = type_element_name( size_type );
	if ( type_name_equal( element_name, &uint8_name ) )
	{
		size = (uint32_t) type_stack_get_u8( stack );
	}
	else if ( type_name_equal( element_name, &uint16_name ) )
	{
		size = (uint32_t) type_stack_get_u16( stack );
	}
	else
	{
		type_input_set_error( in, BAD_ARRAY_SIZE );
		return TYPE_EOF;
	}
	type_stack_pop( stack );
	
	for ( x = 0 ; x < size; x++ )
	{
		type_stack_push( stack );
		
		rc = ma->type->doRead( in, (type_element *) ma->type, NULL );
		if ( rc != TYPE_OK ) return rc;
		
		type_list_add_tail( list, type_stack_remove( stack ) );
	}
	return TYPE_OK;
}

int meta_array_do_write( type_output_stream * out, void * o, type_element * element, void * data )
{
	TRACE( "meta_array_do_write" );
	type_output_set_error( out, NOT_IMPLEMENTED );
	return TYPE_EOF;	
}


